package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.ScenicReply;
import cn.wolfcode.luowowo.comment.repository.IScenicReplyRepository;
import cn.wolfcode.luowowo.comment.service.IScenicReplyService;
import cn.wolfcode.luowowo.scenic.query.ScenicReplyQueryObject;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.List;

@Service
public class ScenicReplyServiceImpl implements IScenicReplyService {

    @Autowired
    private MongoTemplate mongoTemplate;
    @Autowired
    private IScenicReplyRepository repository;

    public ScenicReply save(ScenicReply scenicReply) {
        if(scenicReply.getId() == null){
            scenicReply.setCreateTime(new Date());
        }
        //关联评论
        if(scenicReply.getParentReplyId() != null){
            // 根据回复的评论的id, 查询出其回复的评论对象, 设置到RefComment中
            scenicReply.setParentReply(this.get(scenicReply.getParentReplyId()));
        }

        repository.save(scenicReply);
        return scenicReply;
    }

    public ScenicReply get(String id) {
        return repository.findById(id).get();
    }

    public Page<ScenicReply> query(ScenicReplyQueryObject qo) {
        // 封装查询对象
        Query query = new Query();
        // 条件封装对象
        Criteria criteria = null;
        // 攻略文章条件
        if(qo.getScenicId() != -1){
            criteria = Criteria.where("scenicId").is(qo.getScenicId());
        }else {
            criteria = Criteria.where("scenicId").ne(qo.getScenicId());
        }

        // 关键字查询
        if(qo.getKeyword() != null){
            criteria.and("content").regex("^.*" + qo.getKeyword() + ".*$");
        }

        // 将条件封装对象 整合到 查询对象中
        query.addCriteria(criteria);

        // 通过设置好的查询对象, 查询所有符合条件的评论总条数
        long total = mongoTemplate.count(query, ScenicReply.class);
        // 如果没有查到, 返回一个空分页对象
        if(total == 0){
            return  Page.empty();
        }

        // 分页对象
        Pageable pageable= PageRequest.of(qo.getCurrentPage() - 1, qo.getPageSize());
        // 为查询对象添加分页条件
        query.with(pageable);

        // 排序对象
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        // 为查询对象添加排序条件
        query.with(sort);

        // 分页&排序&高级查询
        List<ScenicReply> list = mongoTemplate.find(query, ScenicReply.class);

        // 返回PageImpl
        // 1. 分页元素集合, 分页对象, 总查询数
        return new PageImpl<ScenicReply>(list, pageable, total);
    }

    @Override
    public List<ScenicReply> getAll(ScenicReplyQueryObject qo) {
        // 封装查询对象
        Query query = new Query();
        // 条件封装对象
        Criteria criteria = null;
        // 攻略文章条件
        if(qo.getScenicCommentId() != null){
            criteria = Criteria.where("commentId").is(qo.getScenicCommentId());
        }
        // 将条件封装对象 整合到 查询对象中
        query.addCriteria(criteria);
        // 排序对象
//        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
//        // 为查询对象添加排序条件
//        query.with(sort);
        List<ScenicReply> list = mongoTemplate.find(query, ScenicReply.class);
        return list;
    }
}
